package bmatch.tech.todo.service.impl;

import bmatch.tech.todo.dao.BmAuthCodeMapper;
import bmatch.tech.todo.dao.BmUserMapper;
import bmatch.tech.todo.model.dataobject.BmUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program:todo
 * @author:LinJinhao
 * @create:2019-01-24 16:28
 **/
@Component
public class EmailTask {

	private final BmUserMapper bmUserMapper;
	private final BmAuthCodeMapper bmAuthCodeMapper;

	@Autowired
	public EmailTask(BmUserMapper bmUserMapper, BmAuthCodeMapper bmAuthCodeMapper) {
		this.bmUserMapper = bmUserMapper;
		this.bmAuthCodeMapper = bmAuthCodeMapper;
	}
	@Scheduled(fixedRate = 3000)
	public void checkCodeTime() throws Throwable{
		List<BmUser> bmUsers = bmUserMapper.selectAll();

		if (bmUsers.size() > 0){
		for (BmUser bmUser : bmUsers){
			if (!bmAuthCodeMapper.selectByUserId(bmUser.getEmail()).equals("")){
				SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
				Date now = format.parse(String.valueOf(new Date()));
				Date old = format.parse(String.valueOf(bmAuthCodeMapper.selectByUserId(bmUser.getEmail()).getGmtCreated()));
				long time = now.getTime() - old.getTime();
				long second = time / 1000;
				if (second > 300){
					bmAuthCodeMapper.deleteByPrimaryKey(bmUser.getId());
				}
			}
		}
	}
	}
}
